﻿using System;
using System.Collections.Generic;
using Missbot.Service;

namespace Missbot.AIModule.Gaming.DriftBottle
{
    /// <summary>
    /// 漂流瓶统计记录
    /// </summary>
    public class DriftBottleAnalyzeRecord : DbBaseEntity
    {
        /// <summary>
        /// 日期字符串
        /// </summary>
        public string DateStr { get; init; }

        /// <summary>
        /// 物品记录字典
        /// </summary>
        public Dictionary<string, int> ItemDic { get; } = new();
    }

    public static class DriftBottleAnalyzeDA
    {
        /// <summary>
        /// 获取今日的统计记录
        /// </summary>
        /// <returns></returns>
        public static DriftBottleAnalyzeRecord GetToday()
        {
            var dstr = DateTime.Now.ToString("yyyyMMdd");
            var rec = MongoService<DriftBottleAnalyzeRecord>.GetOnly(p => p.DateStr == dstr);
            if (rec != null)
            {
                return rec;
            }

            rec = new DriftBottleAnalyzeRecord { DateStr = dstr };
            MongoService<DriftBottleAnalyzeRecord>.Insert(rec);

            return rec;
        }

        /// <summary>
        /// 记录物品信息
        /// </summary>
        /// <param name="item"></param>
        /// <param name="count"></param>
        public static void Record(string item, int count = 1)
        {
            var rec = GetToday();

            if (!rec.ItemDic.ContainsKey(item))
            {
                rec.ItemDic.Add(item, 0);
            }

            rec.ItemDic[item] += count;
            rec.Update();
        }

        /// <summary>
        /// 更新
        /// </summary>
        public static void Update(this DriftBottleAnalyzeRecord entity)
        {
            MongoService<DriftBottleAnalyzeRecord>.Update(entity);
        }
    }
}